//
//  SizeViewController.m
//  SDHuiYuShoping
//
//  Created by luojiao on 2017/5/15.
//  Copyright © 2017年 luojiao. All rights reserved.
//

#import "SizeViewController.h"
#import "UIViewController+KNSemiModal.h"
#import "SizeTableViewCell.h"

@interface SizeViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,ChoseSizeDelegate>

{
    NSArray *classArr;
    NSArray *choseSizeArr;
    NSArray *colorSizeArr;
}
@end

@implementation SizeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    classArr = [[NSArray alloc] initWithObjects:@"规格",@"颜色", nil];
    choseSizeArr = [[NSArray alloc] initWithObjects:@"S",@"M",@"L", nil];
    colorSizeArr = [[NSArray alloc] initWithObjects:@"红色",@"绿色",@"蓝色", nil];
    
    self.tableView.tableFooterView = self.footerView;
    self.tableView.separatorStyle = NO;
    
    self.textFieldBg.image = [UIImage imageNamed:@"number_bg"];
    [self.subButton setImage:[UIImage imageNamed:@"sub_btn_dis"] forState:UIControlStateNormal];
    [self.subButton setImageEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
    [self.addButton setImage:[UIImage imageNamed:@"add_btn"] forState:UIControlStateNormal];
    [self.addButton setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
    [self.closeButton setImage:[UIImage imageNamed:@"close_btn"] forState:UIControlStateNormal];
    [self.closeButton setImageEdgeInsets:UIEdgeInsetsMake(-5, 0, 0, -20)];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mrak - ButtonClick
- (IBAction)subButton:(id)sender {
}

- (IBAction)addButton:(id)sender {
}

- (IBAction)OKButton:(id)sender {
}

- (IBAction)closeBUtton:(id)sender
{
    [self closeViewController];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ((choseSizeArr.count%3) == 0)
    {
        return choseSizeArr.count/3;
    }
    else
    {
        return choseSizeArr.count/3 + 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return classArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;   //接近0的高度
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identfier = @"siezeCell";
    SizeTableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:identfier];
    if (!cell)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:@"SizeTableViewCell" owner:self options:nil][0];
    }
    cell.delegate = self;
    
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            [cell.sButton setTitle:[choseSizeArr objectAtIndex:0] forState:UIControlStateNormal];
            [cell.sButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -80, 0, 0)];

            [cell.mButton setTitle:[choseSizeArr objectAtIndex:1] forState:UIControlStateNormal];
            [cell.mButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -80, 0, 0)];

            [cell.Lbutton setTitle:[choseSizeArr objectAtIndex:2] forState:UIControlStateNormal];
            [cell.Lbutton setTitleEdgeInsets:UIEdgeInsetsMake(0, -80, 0, 0)];

        }
        else
        {
            [cell.sButton setTitle:[choseSizeArr objectAtIndex:3] forState:UIControlStateNormal];
            [cell.mButton setTitle:[choseSizeArr objectAtIndex:1] forState:UIControlStateNormal];
            [cell.Lbutton setTitle:[choseSizeArr objectAtIndex:2] forState:UIControlStateNormal];
        }
    }
    else if (indexPath.section == 1)
    {
        [cell.sButton setTitle:[colorSizeArr objectAtIndex:0] forState:UIControlStateNormal];
        [cell.sButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -80, 0, 0)];
        
        [cell.mButton setTitle:[colorSizeArr objectAtIndex:1] forState:UIControlStateNormal];
        [cell.mButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -80, 0, 0)];
        
        [cell.Lbutton setTitle:[colorSizeArr objectAtIndex:2] forState:UIControlStateNormal];
        [cell.Lbutton setTitleEdgeInsets:UIEdgeInsetsMake(0, -80, 0, 0)];
    }
    
    //cell 没有选中状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 30)];
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel *sizeName = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 120, 30)];
    [headerView addSubview:sizeName];
    sizeName.text = [classArr objectAtIndex:section];
    return headerView;
}


//弹出半截页面的页面回收方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self closeViewController];
}

- (void)closeViewController {
    
    UIViewController * parent = [self.view containingViewController];
    if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
        [parent dismissSemiModalView];
    }
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
